package moodish.Security;

import static org.junit.Assert.*;
import java.util.Date;
import java.util.Hashtable;
import java.util.Stack;
import moodish.dummy.ServerCommDummy;
import org.junit.Test;

public class ServerCommFilterTest {

	private ServerCommFilter filter;
	
	
	
	
	@SuppressWarnings("deprecation")
	@Test
	public void testClientIsConnected() {
		filter = new ServerCommFilter(new ServerCommDummy());
		Hashtable<String, Date> blackList = new Hashtable<String, Date>();
		blackList.put("Andre", new Date());
		blackList.put("Leonardo", new Date());
		blackList.put("Vieira", new Date());
		blackList.put("Ana", new Date(2013,12,10,23,10,10));
		filter.setBlackList(blackList);
		
		
		assertFalse(filter.clientIsConnected("Andre"));
		assertTrue(filter.clientIsConnected("Diogo"));
		assertTrue(filter.clientIsConnected("Ana"));
	}

	@Test
	public void testBanSendTheSameRepeatedly() {
		filter = new ServerCommFilter(new ServerCommDummy());
		Hashtable<String, Stack<MoodishTime>> moodishSend = new Hashtable<String, Stack<MoodishTime>>();
		Stack<MoodishTime> moodishStack = new Stack<MoodishTime>();
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		
		moodishSend.put("Vieira", moodishStack);
		filter.setMoodishSendByUser(moodishSend);
		
		assertEquals(2, filter.verifyAbuseBehavior("Vieira", "Sleepy"));
		assertTrue(filter.getBlackList().containsKey("Vieira"));
		
	}
	
	@Test
	public void testWarningSendTheSameRepeatedly() {
		filter = new ServerCommFilter(new ServerCommDummy());
		Hashtable<String, Stack<MoodishTime>> moodishSend = new Hashtable<String, Stack<MoodishTime>>();
		Stack<MoodishTime> moodishStack = new Stack<MoodishTime>();
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		moodishStack.push(new MoodishTime("Clueless", new Date()));
		
		moodishSend.put("Vieira", moodishStack);
		filter.setMoodishSendByUser(moodishSend);
		
		assertEquals(1, filter.verifyAbuseBehavior("Vieira", "Sleepy"));
		assertFalse(filter.getBlackList().containsKey("Vieira"));
		
	}
	
	
	@Test
	public void testBanSendTenMoodishUnderThirtySeconds() {
		filter = new ServerCommFilter(new ServerCommDummy());
		Hashtable<String, Stack<MoodishTime>> moodishSend = new Hashtable<String, Stack<MoodishTime>>();
		Stack<MoodishTime> moodishStack = new Stack<MoodishTime>();
		
		moodishStack.push(new MoodishTime("Wonderful", new Date()));
		moodishStack.push(new MoodishTime("Hungry", new Date()));
		moodishStack.push(new MoodishTime("Clueless", new Date()));
		moodishStack.push(new MoodishTime("Challenged", new Date()));
		moodishStack.push(new MoodishTime("Disgruntled", new Date()));
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		moodishStack.push(new MoodishTime("Frazzled", new Date()));
		moodishStack.push(new MoodishTime("Good", new Date()));
		moodishStack.push(new MoodishTime("Bored", new Date()));
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		
		moodishSend.put("Vieira", moodishStack);
		filter.setMoodishSendByUser(moodishSend);
		
		assertEquals(4, filter.verifyAbuseBehavior("Vieira", "Cheerful"));
		assertTrue(filter.getBlackList().containsKey("Vieira"));
		
	}
	
	@Test
	public void testWarningSendTenMoodishUnderThirtySeconds() {
		filter = new ServerCommFilter(new ServerCommDummy());
		Hashtable<String, Stack<MoodishTime>> moodishSend = new Hashtable<String, Stack<MoodishTime>>();
		Stack<MoodishTime> moodishStack = new Stack<MoodishTime>();
		
		moodishStack.push(new MoodishTime("Wonderful", new Date()));
		moodishStack.push(new MoodishTime("Hungry", new Date()));
		moodishStack.push(new MoodishTime("Clueless", new Date()));
		moodishStack.push(new MoodishTime("Challenged", new Date()));
		moodishStack.push(new MoodishTime("Disgruntled", new Date()));
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		moodishStack.push(new MoodishTime("Frazzled", new Date()));

		
		moodishSend.put("Vieira", moodishStack);
		filter.setMoodishSendByUser(moodishSend);
		
		assertEquals(3, filter.verifyAbuseBehavior("Vieira", "Cheerful"));
		assertFalse(filter.getBlackList().containsKey("Vieira"));
		
	}
	
	@Test
	public void testSendMoodishMessageCreateNew(){
		filter = new ServerCommFilter(new ServerCommDummy());
		assertEquals(0, filter.verifyAbuseBehavior("Leonardo", "Cheerful"));
		assertFalse(filter.getBlackList().containsKey("Leonardo"));
	}
	
	@Test
	public void testSendMoodishMessageAddTo(){
		filter = new ServerCommFilter(new ServerCommDummy());
		assertEquals(0, filter.verifyAbuseBehavior("Leonardo", "Cheerful"));
		assertEquals(0, filter.verifyAbuseBehavior("Leonardo", "Frazzled"));
		assertFalse(filter.getBlackList().containsKey("Leonardo"));
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void testGarbageCollector() {
		filter = new ServerCommFilter(new ServerCommDummy());
		
		Hashtable<String, Stack<MoodishTime>> moodishSend = new Hashtable<String, Stack<MoodishTime>>();
		
		Stack<MoodishTime> moodishStack = new Stack<MoodishTime>();
		moodishStack.push(new MoodishTime("Wonderful", new Date(2013,12,14,12,00,00)));
		moodishStack.push(new MoodishTime("Disgruntled", new Date(2013,12,14,12,10,00)));
		moodishStack.push(new MoodishTime("Hungry", new Date()));
		moodishStack.push(new MoodishTime("Clueless", new Date()));
		moodishStack.push(new MoodishTime("Challenged", new Date()));
		moodishStack.push(new MoodishTime("Sleepy", new Date()));
		
		moodishSend.put("Vieira", moodishStack);
		
		filter.setMoodishSendByUser(moodishSend);
		
		
		assertEquals(0, filter.verifyAbuseBehavior("Vieira", "Cheerful"));
		assertFalse(filter.getBlackList().containsKey("Vieira"));
		
	}
	
}